//
//  PrepareGuideViewController.h
//  CommunityNannyO2O
//
//  Created by apple on 15/8/13.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "PrepareGuideViewController.h"
#import "APIAdress.h"
#import "MBProgressHUD+YC.h"
#import "MJExtension.h"
#import "AccountTool.h"
#import "AppConfig.h"
#import "QXNetWorkTool.h"
#import "JSDropDownMenu.h"
#import "MJRefresh.h"
#import "PrepareGuideModel.h"
#import "UserDataSingleton.h"
#import "NIDropDown.h"
#import "MenuCell.h"

@interface PrepareGuideViewController()<QXNetWorkToolDelegate, NIDropDownDelegate>


@property (copy,nonatomic) NSString *majorId;
@property (copy,nonatomic) NSString *gradeNum;
@property (weak,nonatomic) IBOutlet UIWebView *webView;


@property (nonatomic,weak) IBOutlet UIButton *button0;
@property (nonatomic,weak) IBOutlet UIButton *button1;

@property (strong, nonatomic) NIDropDown *dropDown;
@property (assign, nonatomic) BOOL isDropDown;

@property (nonatomic,weak) IBOutlet UIView *topView;

@property (nonatomic,strong) NSMutableArray *data1;
@property (nonatomic,strong) NSMutableArray *data2;

@property (nonatomic,assign) NSInteger selectIndex0;
@property (nonatomic,assign) NSInteger selectIndex1;

@property (nonatomic,assign) CGFloat cellHeight;

@property (nonatomic,strong) UIView *maskView;


@end


@implementation PrepareGuideViewController

@synthesize data1;
@synthesize data2;

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self configNavigationItem];
    
    [self configTopView];
    
    self.majorId = @"";
    self.gradeNum = @"";
    
    self.cellHeight = 42;
    
    self.selectIndex0 = -1;
    self.selectIndex1 = -1;
}

-(void)configNavigationItem
{
    self.navigationItem.title = @"备考指南";
    [self.navigationController.navigationBar setTranslucent:NO];
    
    [self.navigationController.navigationBar
     setBackgroundImage:[UIImage imageNamed:@"bg_navigation_bar"]
     forBarMetrics:UIBarMetricsDefault];
    
    //status bar的字体为白色
    //导航栏的背景色是黑色
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],NSFontAttributeName,nil]];
    
    [self configNavigationBackAction:^{
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

-(void)setButtonTextBtn:(UIButton*)btn Title:(NSString*)title
{
    [btn setTitle:title forState:UIControlStateNormal];
}

-(void)getData
{
    [MBProgressHUD showMessage:@"正在加载" toView:self.view];
    QXNetWorkTool *tool = [[QXNetWorkTool alloc]init];
    
    tool.QXNetWorkDelegate = self;
    
    NSString *grade = self.gradeNum;
    if (self.gradeNum.length > 0)
    {
        grade = [self.gradeNum substringWithRange:NSMakeRange(0, 1)];
    }
    
    [tool GetDataFromServerWithPostMethodandInterfaceName:PrepareGuideInterface andParameter:
     @[@{@"major_id":self.majorId,
         @"grade_num":grade}] tag:1];
}


- (void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    
    if (Response == nil)
        return;
    
    NSString *message = Response;
    
    [MBProgressHUD showError:message toView:self.view];
}

- (void)GetSuccessMessage:(id)message andTag:(int)tag
{
    NSString *mes = message;
    
//    [MBProgressHUD showSuccess:mes toView:self.view];
}

-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    
    if (Response == nil)
    {
        return;
    }
    switch (tag)
    {
        case 1:
        {
            for (NSDictionary *dic in Response)
            {
                PrepareGuideModel *model = [PrepareGuideModel prepareGuideWithDict:dic];
                
                [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:model.url]]];
                break;
            }
            
            break;
        }
    }
}

-(void)configTopView
{
    self.data1 = [NSMutableArray arrayWithObjects:@"电吉他", @"电贝司", @"爵士鼓", @"音乐理论",@"不限",nil];
    
    self.data2 = [NSMutableArray arrayWithObjects:@"1级", @"2级", @"3级", @"4级", @"5级", @"6级", @"7级", @"8级", @"9级", @"不限",nil];
    
    [self.topView setFrame:CGRectMake(self.topView.frame.origin.x,
                                      self.topView.frame.origin.y,
                                      [[UIScreen mainScreen] bounds].size.width,
                                      self.topView.frame.size.height)];
    
    self.dropDown = [[NIDropDown alloc]initDropDown:self.topView
                                                  X:0
                                                  Y:0
                                             Height:self.cellHeight * [self.data2 count]];
    
    self.dropDown.delegate = self;
    self.isDropDown = YES;
    
    //遮罩view
    self.maskView = [[UIView alloc] initWithFrame:
                     CGRectMake(0,
                                self.topView.frame.origin.y + self.topView.frame.size.height,
                                [[UIScreen mainScreen] bounds].size.width,
                                [[UIScreen mainScreen] bounds].size.height - self.topView.frame.origin.y - self.topView.frame.size.height)];
    
    self.maskView.hidden = YES;
    self.maskView.backgroundColor = [UIColor blackColor];
    self.maskView.alpha = 0.5;
    [self.view addSubview:self.maskView];
}

- (IBAction)clickBtn0:(id)sender
{
    [self showMenu:0];
}

- (IBAction)clickBtn1:(id)sender
{
    [self showMenu:1];
}

-(void)showMask
{
    self.maskView.hidden = NO;
}

-(void)hideMask
{
    self.maskView.hidden = YES;
}

-(void)showMenu:(NSInteger)column
{
    if (self.isDropDown)
    {
        CGFloat menuHeight;
        
        if (column == 0)
        {
            menuHeight = self.cellHeight * [self.data1 count];
        }
        else
        {
            menuHeight = self.cellHeight * [self.data2 count] - 200;
        }
        
        [self.dropDown setMenuHeight:menuHeight];
        [self.dropDown showDropDownOfColumn:column];
        [self showMask];
    }
    else
    {
        [self.dropDown hideDropDown];
        [self hideMask];
    }
    
    self.isDropDown = !self.isDropDown;
}

///////NIDropDown
- (NSInteger)NIDropDownNumberOfSectionsInTableView:(UITableView *)tableView Column:(NSInteger)column
{
    return 1;
}

-(NSInteger)NIDropDownTableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section Column:(NSInteger)column
{
    if (column == 0)
    {
        return [self.data1 count];
    }
    else {
        return [self.data2 count];
    }
    
    return 0;
}

- (CGFloat)NIDropDownTableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath Column:(NSInteger)column
{
    return self.cellHeight;
}

- (UITableViewCell *)NIDropDownTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath Column:(NSInteger)column
{
    MenuCell *cell = [MenuCell cellWithTableView:tableView];
    
    NSMutableArray *ary;
    if (column == 0)
    {
        ary = self.data1;
    }
    else
    {
        ary = self.data2;
    }
    
    if ([ary count])
    {
        cell.infoLabel.text = [ary objectAtIndex:indexPath.row];
        
        if (column == 0)
        {
            if (self.selectIndex0 != -1 && self.selectIndex0 == indexPath.row)
            {
                cell.check.hidden = NO;
            }
        }
        else
        {
            if (self.selectIndex1 != -1 && self.selectIndex1 == indexPath.row)
            {
                cell.check.hidden = NO;
            }
        }
    }
    
    return cell;
}

- (void)NIDropDownTableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath Column:(NSInteger)column
{
    NSArray *array = [tableView visibleCells];
    
    for (MenuCell *cell in array)
    {
        cell.check.hidden = YES;
    }
    
    MenuCell *cell = (MenuCell *)[tableView cellForRowAtIndexPath:indexPath];
    if (cell)
    {
        cell.check.hidden = NO;
    }
    
    if (column == 0)
    {
        self.selectIndex0 = indexPath.row;
        
        [self setButtonTextBtn:self.button0
                         Title:[self.data1 objectAtIndex:self.selectIndex0]];
        
        switch (indexPath.row)
        {
            case 0:
                self.majorId = [UserDataSingleton shareInstance].quiTa;
                break;
            case 1:
                self.majorId = [UserDataSingleton shareInstance].bass;
                break;
            case 2:
                self.majorId = [UserDataSingleton shareInstance].JazzDrum;
                break;
            case 3:
                self.majorId = [UserDataSingleton shareInstance].musicBase;
                break;
            case 4:
                self.majorId = @"";
                break;
        }
    }
    else if(column == 1)
    {
        self.selectIndex1 = indexPath.row;
        
        [self setButtonTextBtn:self.button1
                         Title:[self.data2 objectAtIndex:self.selectIndex1]];
        
        if (indexPath.row == ([self.data2 count] - 1))
        {
            self.gradeNum = @"";
        }
        else
        {
            self.gradeNum = [self.data2 objectAtIndex:indexPath.row];
        }
    }
    
    self.isDropDown = !self.isDropDown;
    [self hideMask];
    
    [self getData];
}

-(void)useMethodToFindBlackLineAndHind
{
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    
    blackLineImageView.hidden = YES;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self configNavigationItem];
    
    [self useMethodToFindBlackLineAndHind];
}

-(void)viewWillDisappear:(BOOL)animated
{
    
}


@end











































